package com.magelink.service;


import com.magelink.config.IsvConfig;
import com.magelink.core.ResponseMsg;
import com.magelink.domain.ResultCodeEnum;
import com.magelink.pojo.AppInfo;
import com.magelink.pojo.IsvInstance;
import com.magelink.util.SecureUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;



/**
 * SaaS商品接入云市场
 *
 * @author 梁卓
 */

@Service
@AllArgsConstructor
@Slf4j
public class IsvService {
    @Autowired
    private IsvConfig isvConfig;


    public ResponseMsg instance(IsvInstance isvInstance) {
        System.out.println(isvConfig.getAccessKey());
        ResponseMsg responseBody = new ResponseMsg();
        // 校验通知消息的合法性
        boolean verification = SecureUtils.verificationRequestParams(
                BeanUtil.beanToMap(isvInstance, false, true)
                , isvConfig.getAccessKey(), -1);
        if (!verification) {
            responseBody.setResultCode(ResultCodeEnum.authFailed);
            return responseBody;
        }

        // 响应回来的
        responseBody.setInstanceId(isvInstance.getBusinessId());
        responseBody.setResultCode(ResultCodeEnum.success);

        AppInfo appInfo = new AppInfo();
        appInfo.setFrontEndUrl("");//http://www.isvserver.com
        appInfo.setAdminUrl("");//http://www. isvserver.com
        appInfo.setUserName(SecureUtils.generateSaaSUsernameOrPwd(isvConfig.getAccessKey(), "user1000", 256));
        appInfo.setPassword(SecureUtils.generateSaaSUsernameOrPwd(isvConfig.getAccessKey(), "user1000", 256));
        responseBody.setAppInfo(appInfo);
        return responseBody;
    }

    /**
     * 新购场景
     */
    private void newInstanceScene(IsvInstance isvInstance) {
        log.info("新购场景:");
    }

    /**
     * 续费场景
     */
    private void refreshInstanceScene() {
        log.info("续费场景:");
    }

    /**
     * 商品过期
     */
    private void expireInstanceScene() {
        log.info("商品过期场景:");
    }

    /**
     * 商品资源释放
     */
    private void releaseInstanceScene() {
        log.info("商品资源释放场景:");
    }

    /**
     * 商品升级
     */
    private void upgradeScene() {
        log.info("商品升级场景:");
    }

    /**
     * 实例是否已存在,并且有效
     *
     * @param orderId
     * @return
     */
    private boolean isExist(String orderId) {

        return true;
    }


}
